#include "ISEA4DImpl.cuh"
#include"../Projection/Project.cuh"

using namespace NNU::DGGS::Kernel::ISEA4D;
using namespace NNU::DGGS::Kernel::Projection;

__DGGS_CPGPU__  void CuISEA4DImpl::init()
{
    this->isea = new Project();
}

__DGGS_CPGPU__ void CuISEA4DImpl::release()
{
    delete this->isea;
}

__DGGS_CPGPU__ bool CuISEA4DImpl::decode(DGGSElementCode code, GeoCoord& coord)
{
    double lon = 0;
    double lat = 0;
    switch (code.elementType)
    {
        case DGGSElementType::Voxel:
        case DGGSElementType::Cell:
        {
            this->isea->RC2LBCenter(DGGSGridType::DGGS_ISEA4D, code.basePartition, code.row, code.col, code.level, lon,
                                    lat);
            coord.lon = lon;
            coord.lat = lat;
            break;
        }
        case DGGSElementType::Edge:
        {
            code.col=code.col/2;
            double* vertexes = new double[3];
            this->isea->RC2LBVertex(code.basePartition, code.row, code.col, code.level, DGGSGridType::DGGS_ISEA4D, vertexes);
            coord.lon = vertexes[0];
            coord.lat = vertexes[1];
            delete[]vertexes;
            break;
        }
        case DGGSElementType::Vertex:
        {
            double* vertexes = new double[3];
            this->isea->RC2LBVertex(code.basePartition, code.row, code.col, code.level, DGGSGridType::DGGS_ISEA4D, vertexes);
            coord.lon = vertexes[0];
            coord.lat = vertexes[1];
            delete[]vertexes;
            break;
        }
        default:
            printf("Entered a wrong type of element!\n");
            return false;
    }
    return true;
}

__DGGS_CPGPU__ bool CuISEA4DImpl::encode(GeoCoord coord, DGGSElementCode& code)
{
    long long int row, col;
    int basePartition = -1;
    this->isea->LB2RC(coord.lon, coord.lat, code.level, DGGSGridType::DGGS_ISEA4D, basePartition, row, col);
    code.basePartition = basePartition;
    code.elementType = DGGSElementType::Cell;
    code.row = row;
    code.col = col;
    return true;
}